<?php
class CBlock extends CHaveSettingComponent
{
	static $current;	
	public $module;
	public $children;
	
	function __construct($data, $module = false)
	{
		parent::__construct($data);
		$this->module = $module;
		if($this->module)
		{
			$last = &CBlock::$current;
			CBlock::$current = &$this;
			$this->module->init();
			CBlock::$current = &$last;
		}
	}
	function draw()
	{
		$last = &CBlock::$current;
		CBlock::$current = &$this;
		echo '<script type="text/javascript">SYS.currentBlockId = '.$this->module->id.';</script>';
		if(CPage::$edit_mode)
		{
			echo '<li><a href="javascript:currentEditPageBlock.editClick('.$this->id.');">'.$this->module->name.'</a>';
			if(CFile::exists($this->path().'/regions.tpl'))
			{
				CForm($this->path().'/regions.tpl')->parse($this->settings);
			}
			elseif(isset($this->data['blocks']) and $this->data['blocks'])
			{
				$regions = array();
				foreach($this->data['blocks'] as &$block)
				{
					$regions[isset($block['region'])?$block['region']:'center'] = 1;
				}
				
				$layout = '';
				foreach($regions as $region=>$nothing)
				{
					$layout .= '{region(\''.$region.'\')}';
				}
				eval('?>'.CFormLayout($layout)->parse().'<?php ');
			}
			echo '</li>';
		}
		else
		{
			$this->module->draw();
		}
		CBlock::$current = &$last;
	}
	function get_tooltip()
	{
		return $this->module->data['name'];
	}
	function submit()
	{
		if(request('submit_form_id') == $this->module->id)
		{
			$last = &CBlock::$current;
			CBlock::$current = &$this;
			$this->module->submit();
			CBlock::$current = &$last;
		}
	}
	function path()
	{
		return $this->module->path();
	}
}
function &module()
{
	if(CBlock::$current)
	{
		return CBlock::$current;
	}
	else
	{
		$block = new CBlock(array('id'=>0));
		return $block;
	}
}
function setting($name, $default = NULL)
{
	return module()->setting($name, $default);
}
function module_path()
{
	return module()->path();
}
?>